package com.jwaoo.mobile.view.register.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.design.widget.TextInputLayout;
import android.support.v4.view.GestureDetectorCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.dx.as.ToolBarOptions;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.FragmentUtils;
import com.dx.as.common.JWConstant;
import com.dx.as.common.string.StringUtil;
import com.dx.as.fragment.TFragment;
import com.dx.as.utils.AESSecurityUtil;
import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.view.register.IParamListener;

/**
 * Created by jwaoo on 2017/10/14.
 */

public class AccountFragment extends TFragment{
    private EditText edt_email;
    private EditText edt_password;
    private TextInputLayout lb_password;
    private IParamListener listener;
    private Button button;
    private Drawable [] drawablePassword;
    private Drawable [] drawableEmail;
    private boolean [] isComplete = new boolean[2];
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (IParamListener) context;
    }

    @Override
    public void initView() {
        edt_email = findView(R.id.edt_email);
        edt_password = findView(R.id.edt_password);
        lb_password = findView(R.id.lb_password);
        button = findView(R.id.btn_next);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = edt_email.getText().toString();
                String password = edt_password.getText().toString();
                try {
                    password = new String(AESSecurityUtil.encrypt(password, JWConstant.KEY_AES));
                }catch (Exception ex){}
                listener.saveEmail(email,password);
                showKeyboard(false);
                FragmentUtils.goToFragment(getActivity(),CountryFragment.class,R.id.register_container);
            }
        });
        edt_email.addTextChangedListener(textWatcher);
        edt_password.addTextChangedListener(textWatcher);
        if (Build.VERSION.SDK_INT >= 21) {
            drawablePassword = new Drawable[]{getResources().getDrawable(R.drawable.signup_hide_password_icon),getResources().getDrawable(R.drawable.signup__show_password_icon)};
            drawableEmail = new Drawable[]{getResources().getDrawable(R.drawable.signup_error_icon),getResources().getDrawable(R.drawable.signup_right_icon)};
        }

        final GestureDetectorCompat mDetector = new GestureDetectorCompat(ChatHelpter.getContext(),onGestureListener);
        edt_password.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mDetector.onTouchEvent(event);
                return false;
            }
        });

        findView(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    @Override
    public int setLayoutI() {
        return R.layout.fragment_register;
    }

    @Override
    public void dataOpion() {

    }

    @Override
    public void initRpo() {

    }

    @Override
    public void onResume() {
        super.onResume();
    }


    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (edt_email.isFocused()){
                checkEmail(s.toString());
            }else if (edt_password.isFocused()){
                checkPassword(s.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    private void checkEmail(String email){
        if (CommonUtils.chekEmail(email)){
            isComplete[0] = true;
            if (changeButtonState()) button.setEnabled(true);
            setDrawable(edt_email , drawableEmail[1]);
            return;
        }
        isComplete[0] = false;
        setDrawable(edt_email , drawableEmail[0]);
        button.setEnabled(false);
    }

    private void checkPassword(String password){
        if (!StringUtil.isEmpty(password) && password.trim().length() >= 6) {
            lb_password.setErrorEnabled(false);
            isComplete[1] = true;
            if (changeButtonState()) button.setEnabled(true);
            return;
        }
        lb_password.setErrorEnabled(true);
        lb_password.setError(getResources().getString(R.string.error_pasword));
        isComplete[1] = false;
        button.setEnabled(false);
    }


    private boolean changeButtonState(){
        return isComplete[0] && isComplete[1];
    }

    private boolean isHide = true;

    private  GestureDetector.OnGestureListener onGestureListener = new GestureDetector.OnGestureListener(){
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (e.getX() > (edt_password.getRight() - drawablePassword[0].getMinimumWidth() -30)){
                isHide = !isHide;
                setDrawable(edt_password , isHide ? drawablePassword[0] : drawablePassword[1]);
                edt_password.setTransformationMethod(isHide ? PasswordTransformationMethod.getInstance() : HideReturnsTransformationMethod.getInstance());
            }
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    };

    private void setDrawable(EditText v ,Drawable drawable){
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());
        v.setCompoundDrawables(null,null,drawable,null);
    }
}
